package cn.syncrond.tcm.core.pool;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description: 暂停支持
 * @author 刘振兴
 * @date 2016-11-17
 */
public class PauseSupport implements IPausable {

	/**
	 * 暂停状态
	 */
	protected boolean isPaused;
	
	/**
	 * 排斥锁
	 */
	protected ReentrantLock pauseLock = new ReentrantLock();
	
	/**
	 * 监控条件
	 */
	protected Condition unpaused = pauseLock.newCondition();
	
	@Override
	public void pause() {
		pauseLock.lock();
		try {
			isPaused = true;
		} finally {
			pauseLock.unlock();
		}
	}

	@Override
	public void resume() {
		pauseLock.lock();
		try {
			isPaused = false;
			unpaused.signalAll();
		} finally {
			pauseLock.unlock();
		}
	}
}
